package com.example.petshopping.ui.follows.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.petshopping.R;
import com.example.petshopping.ui.follows.model.Store;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class FollowedStoreAdapter extends RecyclerView.Adapter<FollowedStoreAdapter.FollowedStoreViewHolder> {

    private Context context;
    private List<Store> storeList;
    private List<Store> filteredList;
    private OnStoreActionListener listener;

    public interface OnStoreActionListener {
        void onEnterStoreClick(Store store);
    }

    public FollowedStoreAdapter(Context context, List<Store> storeList, OnStoreActionListener listener) {
        this.context = context;
        this.storeList = storeList != null ? storeList : new ArrayList<>();
        this.filteredList = new ArrayList<>(this.storeList);
        this.listener = listener;
    }

    @NonNull
    @Override
    public FollowedStoreViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_followed_store, parent, false);
        return new FollowedStoreViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull FollowedStoreViewHolder holder, int position) {
        Store store = filteredList.get(position);
        
        // 设置店铺名称
        holder.tvStoreName.setText(store.getName());
        
        // 设置店铺头像
        holder.ivStoreAvatar.setImageResource(store.getAvatarResourceId());
        
        // 设置是否显示新店标签
        if (store.isNewStore()) {
            holder.tvNewStoreBadge.setVisibility(View.VISIBLE);
        } else {
            holder.tvNewStoreBadge.setVisibility(View.GONE);
        }
        
        // 设置进店按钮点击事件
        holder.btnEnterStore.setOnClickListener(v -> {
            if (listener != null) {
                listener.onEnterStoreClick(store);
            }
        });
    }

    @Override
    public int getItemCount() {
        return filteredList.size();
    }
    
    // 过滤关注的店铺
    public void filterFollowedStores() {
        filteredList.clear();
        for (Store store : storeList) {
            if (store.isFollowed()) {
                filteredList.add(store);
            }
        }
        notifyDataSetChanged();
    }
    
    // 更新数据
    public void updateData(List<Store> newStores) {
        this.storeList.clear();
        if (newStores != null) {
            this.storeList.addAll(newStores);
        }
        filterFollowedStores();
    }

    static class FollowedStoreViewHolder extends RecyclerView.ViewHolder {
        CircleImageView ivStoreAvatar;
        TextView tvStoreName, tvNewStoreBadge;
        Button btnEnterStore;

        FollowedStoreViewHolder(@NonNull View itemView) {
            super(itemView);
            ivStoreAvatar = itemView.findViewById(R.id.iv_store_avatar);
            tvStoreName = itemView.findViewById(R.id.tv_store_name);
            tvNewStoreBadge = itemView.findViewById(R.id.tv_new_store_badge);
            btnEnterStore = itemView.findViewById(R.id.btn_enter_store);
        }
    }
} 